package org.example.util;

import lombok.Data;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: scout.du
 * @date: 2024/03/26 10:23
 **/
@Data
public class DateUtils {

    public static final String STANDARD_DATE = "yyyy-MM-dd";
    public static final String STANDARD_SEC = "yyyy-MM-dd HH:mm:ss";
    public static final String STANDARD_MILL = "yyyy-MM-dd HH:mm:ss:SSS";
    public static final String ARGOCD_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";

    private static Map<String, DateTimeFormatter> map = new HashMap<>();

    private String formatStr;

    public static DateUtils instance(String format){

        DateUtils dateUtils = new DateUtils();
        dateUtils.setFormatStr(format);
        return dateUtils;
    };

    private static DateTimeFormatter getFormatter(String pattern){
        DateTimeFormatter cacheFormatter = map.get(pattern);
        if (cacheFormatter==null){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            map.put(pattern,formatter);
            return formatter;
        }
        return cacheFormatter;
    }

    public static String format(Date date,String pattern){

        return format(date.getTime(),pattern);
    }

    public static String format(Long timestamp,String pattern){

        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zoneId = ZoneId.systemDefault();//东八区北京时间
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        return format(localDateTime,pattern);
    }

    public static String format(LocalDateTime localDateTime, String pattern){

        DateTimeFormatter formatter = getFormatter(pattern);
        return localDateTime.format(formatter);
    }

    public String format(Date date){
        return format(date.getTime());
    }

    public String format(Long timestamp){
        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zoneId = ZoneId.systemDefault();//东八区北京时间
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        return format(localDateTime);
    }

    public String format(LocalDateTime localDateTime){
        DateTimeFormatter formatter = getFormatter(formatStr);
        return localDateTime.format(formatter);
    }

    public static Date parseToDate(String time,String pattern){

        return new Date(parseToLong(time,pattern));
    }

    public static Long parseToLong(String time,String pattern){

        LocalDateTime localDateTime = parseToLocalDateTime(time, pattern);
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }


    public static LocalDateTime parseToLocalDateTime(String time,String pattern){

        return LocalDateTime.parse(time, getFormatter(pattern));
    }

    public static LocalDateTime convertToLocalDateTime(Long timestamp){

        Instant instant = Instant.ofEpochMilli(timestamp);
        ZoneId zoneId = ZoneId.systemDefault();//东八区北京时间
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    public static Long convertToLong(LocalDateTime localDateTime){

        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }
}
